//
//  AllTravelTableViewCell.m
//  艾听歌
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AllTravelTableViewCell.h"
#import "TravelCollectionViewCell.h"
#import "DetailViewController.h"

#import "Product.h"

@interface AllTravelTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation AllTravelTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        //数组初始化
        self.dataArr = [NSMutableArray array];
        
        //铺控件
        self.titleImg = [[UIImageView alloc] init];
//        self.titleImg.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.titleImg];
        self.titleImg.userInteractionEnabled = YES;
        
        self.titleLabel = [[UILabel alloc] init];
//        self.titleLabel.backgroundColor = [UIColor yellowColor];
        [self.titleImg addSubview:self.titleLabel];
        self.titleLabel.userInteractionEnabled = YES;
        
        self.secTitLabel = [[UILabel alloc] init];
//        self.secTitLabel.backgroundColor = [UIColor purpleColor];
        [self.titleImg addSubview:self.secTitLabel];
        self.secTitLabel.userInteractionEnabled = YES;
        
        
        //给第一种cell上的控件加轻拍方法,让页面跳转
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
       
        [self.titleImg addGestureRecognizer:tap1];
        [self.titleLabel addGestureRecognizer:tap2];
        [self.secTitLabel addGestureRecognizer:tap3];
        
        
        //创建collectionView
        [self createCollectionView];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleImg.frame = CGRectMake(0, 0, WIDTH, 200);
    self.titleLabel.frame = CGRectMake(self.titleImg.frame.size.width / 2 - 100, self.titleImg.frame.size.height / 2 - 20, 200, 40);
    self.secTitLabel.frame = CGRectMake(self.titleImg.center.x - 100, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height +10, 200, 20);
    self.titleLabel.textColor = [UIColor whiteColor];
    // 字体加粗及字号
    [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:25]];
    self.secTitLabel.textColor = [UIColor whiteColor];
    self.secTitLabel.font = [UIFont systemFontOfSize:13];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.secTitLabel.textAlignment = NSTextAlignmentCenter;
    
    self.travelCollectionView.frame = CGRectMake(5, self.titleImg.frame.size.height + 30, self.contentView.frame.size.width, 210);
    //毛玻璃效果
    //    UIBlurEffect * effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    //    UIVisualEffectView *view=[[UIVisualEffectView alloc] initWithEffect:effect];
    //    view.frame=CGRectMake(0, 160, 375 , 60);
    //    view.alpha=0.8; //模糊程度
    //    [self.titleImg addSubview:view];
}

- (void)setTableCellModel:(AllTravel *)tableCellModel{
    
    _tableCellModel = tableCellModel;
    
    
    [self.titleImg sd_setImageWithURL:[NSURL URLWithString:self.tableCellModel.cover_image_url] placeholderImage:[UIImage imageNamed:@"holderr"]];
    
    self.titleLabel.text = self.tableCellModel.title;
    
    self.secTitLabel.text = self.tableCellModel.sub_title;
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (Product *proModel in self.tableCellModel.products) {
        
        [array addObject:proModel];
        
    }
    
    self.dataArr = [NSMutableArray arrayWithArray:array];
    
    
    //重写set方法的时候 ,将collectionview刷新一下
    [self.travelCollectionView reloadData];
    
}


//轻拍手势的方法实现
- (void)tapAction{
    
    
    self.toDetailBlock(self.tableCellModel);
    
    
}




// 创建小collectionView
- (void)createCollectionView {
    // layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.itemSize = CGSizeMake(120, 170);
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.travelCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, self.titleImg.frame.size.height + 40, self.contentView.frame.size.width, 190)collectionViewLayout:layout];
    self.travelCollectionView.delegate = self;
    self.travelCollectionView.dataSource = self;
//        self.travelCollectionView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:254 / 255.0 blue:249 / 255.0 alpha:1];
    self.travelCollectionView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);

    [self.contentView addSubview:_travelCollectionView];
    [self.travelCollectionView registerClass:[TravelCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}


// item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
//    return 5;
    
    return self.dataArr.count;
    
}

// cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TravelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//        cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.contentView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFF6EB4,0x9C9C9C);
    cell.contentView.layer.cornerRadius = 6;
    cell.contentView.layer.masksToBounds = YES;
    
    cell.collectionCellModel = self.dataArr[indexPath.row];
    
    return cell;
}

// 寻找响应者
// view中是不能进行UIViewController的push，pop等操作的，若进行跳转操作，一般是用代理，block,通知等实现，那如何实现在ViewController的subView中实现跳转操作呢，其实只要获取该view所在的ViewController即可。
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
          //通过响应者链，获得view所在的视图控制器
        UIResponder *nextResponder = [next nextResponder];
         //判断响应者对象是否是视图控制器类型
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}




@end
